import 'package:imperius_insights/entity/department.dart';
import 'package:imperius_insights/entity/hr.dart';
import 'package:imperius_insights/modules/scope/service.dart';
import 'package:shared_preferences/shared_preferences.dart';

const KEY_SCOPE_CURRENT_DEPARTMENT_ID = 'KEY_SCOPE_CURRENT_DEPARTMENT_ID_v200506';
const KEY_SCOPE_CURRENT_HUMAN_ID = 'KEY_SCOPE_CURRENT_HUMAN_ID';

Map<String, String> humansCache = Map<String, String>.from({
  //
  'meng.jiang': '姜萌',
  'yamm.wang': '王亚美',
  'shuang.xu': '徐爽',
  'huangxinhx.huang': '黄信',
  'xipengwill.wang': '王希朋',
  'yan.qi': '祁焱',
  //
  'yu.cui': '崔宇',
  'wuyun.wu': '武蕴',
  'xiangweizxw.zhang': '张祥伟',
  'caihua.wang': '王才华',
  //
  'lijianjun.li': '李剑君',
  'jiamei.yao': '姚佳梅',
  'lihua.mao': '毛丽华',
  'yabingzyb.zhang': '张亚兵',
  //
  'xinju.dan': '淡新举',
  'shiyuan.xiong': '熊诗远',
  'ssong.li': '李嵩',
  'blake.li': '李智勇',
  'jingqi.li': '李婧琪',
  //
  'taost.sun': '孙涛',
  'chuanyi.xiao': '肖传逸',
  'shipeng.song': '宋世鹏',
  'liming.shi': '施黎明',
  'zhou.yang': '杨洲',
  'jialu.yuan': '袁佳露',
  'shenjin.cai': '蔡沈锦',
  'jiel.lu': '卢杰',
  'whailong.wang': '王海龙',
  'xiangdong.liu': '刘向东',
  //
  'dongpeng.yan': '阎东鹏',
  'xiao.hu': '胡晓',
  'chengc.shu': '舒成',
  'shuang.xie': '谢爽',
  'yping.yang': '杨萍',
  'huijunwhj.wang': '王慧钧',
  'elva.zhang': '张扬Elva',
  'gongyuqa.gong': '宫宇',
  'lul.song': '宋璐',
  //
  'jingli.deng': '邓菁莉',
  'lixiaowei.li': '李晓伟',
  'xiangyi.shi': '史湘翊',
  'hongyuan.li': '李红园',
  'wangjingyi.wang': '王静怡',
  'weizhong.cheng': '程维中',
  'quinn.zhang': '张群',
  'yingbin.li': '李英滨',
  'haoxuan.qu': '区浩轩',
  //
  'lingshanl.sun': '孙凌山',
  'catherine.yang': '杨彤',
  'cassie.zhang': '张慧芳',
  'yanchao.huo': '霍艳超',
  'braelyn.liang': '梁博',
  'wangjiajane.wang': '王佳',
  'jianwen.ren': '任健文',
  'yingchan.qiao': '乔英婵',
  'wangliang.wang': '王亮',
  'shumin.du': '杜书敏',
  'bubles.zhang': '张芳',
  'jianw.li': '李剑威',
  'yawenfd.tian': '田雅文',
  'jiafive.li': '李佳',
  'wenya.pi': '皮文雅',
  'xuechao.shen': '申学超',
  'ziliang.jiang': '蒋子良',
  'shen.yan': '闫屾',
  'allen.yang': '杨博文',
  'junzhong.chen': '陈俊仲',
  'tt.feng': '凤婷',
  'fugang.huang': '黄甫刚',
  'wenjin.li': '李文瑾',
  'yuanwei.zhou': '周元炜',
  'nostalie.zhang': '张俭',
  'weisw.shi': '史伟',
  'chengyi.miao': '苗诚意',
  'qingsong.wei': '魏青松',
  'xinyanx.zhang': '张鑫彦',
  'lingling.chen': '陈玲玲',
  'yuzy.zhao': '赵玉',
  'hq.han': '韩青',
  'naiwen.hu': '胡乃文',
  'weiqi.zhao': '赵伟奇',
  'taylor.wang': '王晓龙',
  'sanxy.zhang': '张悦',
  'yaod.deng': '邓尧',
  'zechuan.guo': '郭泽川',
  'lanwl.wang': '王岚',
  'zhiguang.xia': '夏志光',
  'chenggang.xu': '徐成刚',
  'jiaxin.zhao': '赵嘉欣',
  'jinbo.han': '韩金波',
  'yanxiang.ma': '马艳香',
  'haoxue.li': '李皓雪',
  'chongyuan.guo': '郭崇源',
  'sihui.zhang': '张思慧',
  'qiuyang.zhu': '朱秋阳',
  'zhiwen.xue': '薛智文',
  'xiaoyi.liu': '刘小奕',
  'canna.chen': '陈娜',
  'shengfeng.jiang': '蒋盛锋',
  'lihuimin.li': '李慧敏',
  'hexin.zhang': '张鹤馨',
  'wangleiwl.wang': '王蕾',
  'hufangfang.hu': '胡芳芳',
  'chenhui.zheng': '郑晨辉',
  'zhongyuan.liu': '刘中元',
  'wiley.zhao': '赵卫',
  'hunter.zhang': '张昊',
  'xinghe.li': '李兴赫',
  'chunmeicm.zhang': '张春梅cm',
  'linalina.li': '李娜',
  'zllei.zhang': '张磊',
  'ychunyu.yin': '印春雨',
  'jiangxinyu.jiang': '姜欣羽xy',
  'zqiang.zhang': '张强',
  'fefe.yang': '杨斐斐',
  'fenqing.zhao': '赵芬庆'
});
const DEFAULT_DEPARTMENT_ID = '机票目的地事业群,业务研发,机票业务研发,用户产品,Android';

class Domain {
  Service _service;
  Map<String, List<HumanResource>> _cache;

  Domain() {
    _service = Service();
    _cache = Map<String, List<HumanResource>>();
  }

  ///
  ///UNKOWN(0, "未知类型"),
  // INTERNAL(1, "国内机票"),
  // INTERNATIONAL(2, "国际机票"),
  // CUSTOM_PRODUCT(3, "用户产品"),
  // ETRAVEL(4, "旅行交通"),
  // FUWU_PLATFORM(5, "服务平台"),
  // DEVELOP(6, "战略发展"),
  // SELF_BUSINESS(7, "自营业务");
  Department get _departmentAdr => Department(id: '机票目的地事业群,业务研发,机票业务研发,用户产品,Android', displayName: '用户产品Android');
  Department get _departmentIos => Department(id: '机票目的地事业群,业务研发,机票业务研发,用户产品,iOS', displayName: '用户产品iOS');

  List<Department> get departments => [
        Department(id: null, displayName: '用户产品'),
        _departmentAdr,
        _departmentIos,
        Department(id: '机票目的地事业群,业务研发,机票业务研发,用户产品,前端', displayName: '用户产品FE'),
        Department(id: '机票目的地事业群,业务研发,机票业务研发,用户产品,Server端', displayName: '用户产品Server端'),
        Department(id: '机票目的地事业群,业务研发,机票业务研发,用户产品,客户端聚合', displayName: '客户端联盟定制', combinedDepartments: [_departmentAdr, _departmentIos]),
        Department(id: null, displayName: '国内机票'),
        Department(id: '机票目的地事业群,业务研发,机票业务研发,国内机票,报价交易', displayName: '国内报价交易'),
        Department(id: '机票目的地事业群,业务研发,机票业务研发,国内机票,增值营销', displayName: '国内增值营销'),
        Department(id: '机票目的地事业群,业务研发,QA中心,国内机票', displayName: '国内QA'),
        Department(id: null, displayName: '国际机票'),
        Department(id: '机票目的地事业群,业务研发,机票业务研发,国际机票,主系统', displayName: '国际主系统'),
        Department(id: '机票目的地事业群,业务研发,机票业务研发,国际机票,技术创新', displayName: '国际技术创新'),
        Department(id: '机票目的地事业群,业务研发,机票业务研发,国际机票,报价组', displayName: '国际报价组'),
        Department(id: '机票目的地事业群,业务研发,机票业务研发,国际机票,交易', displayName: '国际交易'),
        Department(id: '机票目的地事业群,业务研发,机票业务研发,国际机票,基础数据', displayName: '国际基础数据'),
        Department(id: '机票目的地事业群,业务研发,QA中心,国际机票', displayName: '国际QA'),
        Department(id: null, displayName: '服务平台'),
        Department(id: '机票目的地事业群,机票目的地事业群,业务研发,服务平台,前端组', displayName: '服务平台前端'),
        Department(id: '机票目的地事业群,机票目的地事业群,业务研发,服务平台,智能服务', displayName: '服务平台智能服务'),
        Department(id: '机票目的地事业群,机票目的地事业群,业务研发,服务平台,国内售后', displayName: '服务平台国内售后'),
        Department(id: '机票目的地事业群,机票目的地事业群,业务研发,服务平台,国际售后', displayName: '服务平台国际售后'),
      ];

  Department queryDepartment({String departmentId}) {
    return departments.firstWhere((d) => d.id == departmentId);
  }

  HumanResource queryHumanInCache(String humanId) {
    if (humanId == null) {
      return null;
    }
    return HumanResource(
      humansCache[humanId],
      humanId,
    );
  }

  Future<bool> saveDepartmentId(String id) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return await prefs.setString(KEY_SCOPE_CURRENT_DEPARTMENT_ID, id);
  }

  Future<String> getDepartmentId() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return prefs.getString(KEY_SCOPE_CURRENT_DEPARTMENT_ID) ??
        DEFAULT_DEPARTMENT_ID;
  }

  Future<bool> hasDepartmentId() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return prefs.containsKey(KEY_SCOPE_CURRENT_DEPARTMENT_ID);
  }

  Future<String> getScopeHumanId() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return prefs.getString(KEY_SCOPE_CURRENT_HUMAN_ID);
  }

  Future<bool> saveScopeHumanId(String id) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return prefs.setString(KEY_SCOPE_CURRENT_HUMAN_ID, id);
  }

  Future<void> deleteCurrentHuman() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return prefs.remove(KEY_SCOPE_CURRENT_HUMAN_ID);
  }

  Future<List<HumanResource>> queryHumansByDepartment(String departmentId) async {
    if (_cache.containsKey(departmentId)) {
      return Future.value(_cache[departmentId]);
    }
    final result = await _service.queryHumansByDepartment(departmentId);
    _cache[departmentId] = result;
    return result;
  }
}
